﻿$(function () {
    $('.tooltipped').tooltip({ delay: 50 });
    $('.modal-trigger').leanModal();
});

function done() {
    var key = $("#search").val().trim();
    if (key == "") {
        Materialize.toast('你不是还没有写av号么', 3000, 'rounded');
        return;
    } else {
        //正则匹配为数字或正则验证
        //var patt1 = new RegExp("(av|AV|Av|aV){0,1}\d{1,8}");
        if (isNaN(key)) {
            var patt1 = key.substr(0, 2);
            if (patt1 == "av" || patt1 == "AV" || patt1 == "Av" || patt1 == "aV") {
                if (isNaN(key.substr(2, key.length - 2))) {
                    Materialize.toast('有必要怀疑你的号码是不是打错了', 3000, 'rounded');
                    return;
                }
            } else {
                Materialize.toast('有必要怀疑你的号码是不是打错了', 3000, 'rounded');
                return;
            }
        }
    }
    $("#main").attr('style', 'display:none');
    $("#kaip").slideUp(500);
    $("#error").slideUp(500);
    setTimeout(function () { $("#main").removeClass().addClass('animated zoomIn').attr('style', 'display:block'); }, 10);
    //初始化
    $("#titles").html("正在加载中");
    //$("#imgurl").attr('src', "/Content/46201453_1.gif");
    $("#imgurl").html(ReferrerKiller.imageHtml('/Content/46201453_1.gif'));
    $("#px").html("计算中");
    $("#urlAdd").val("正在查找图片地址");
    $('#foot_Copy').attr('data-clipboard-text', "");
    $("#foot_DownImg").attr({ 'href': "", 'download': "哔哩哔哩 (゜-゜)つロ 干杯" });
    $("#other").attr('href', '');
    $("#foot_Look").attr('href', '');
    $("#foot_baiduImg").attr('href', '');
    $("#foot_sauImg").attr('href', '');
    $("#foot_tinImg").attr('href', '');
    $("#foot_whatImg").attr('href', '');
    $("#foot_multiImg").attr('href', '');
    setTimeout(function () {
        if ($("#px").html() == "计算中") {
            Materialize.toast('输入的AV号可能有问题或者没有此视频<br/>即将刷新本页面', 3000, 'rounded');
            setTimeout(function () {
                window.location.reload();
            }, 3000);
        }
    }, 10000);
    //获取数据
    $.post('Index/GetInfo',
        { 'url': key },
        function (data) {
            var info = eval('(' + data + ')');
            console.log(info);
            if (info.error == "false" || info.src == "") {
                $("#titles").html(info.title);
                $("#imgurl").html(ReferrerKiller.imageHtml(info.imgUrl));

                //$("#imgurl").attr('src', info.imgUrl);
                var img = new Image();
                img.src = info.imgUrl;
                img.onload = function () {
                    $("#px").html(img.width + "X" + img.height);
                    var width = $("#main").width();
                    if (img.width > width) img.width = width;
                    $("#imgurl").attr('width', img.width);
                }
                $($("#search")[0]).blur();
                $("#urlAdd").val(info.imgUrl);
                $('#foot_Copy').attr('data-clipboard-text', info.imgUrl);
                $("#foot_DownImg").attr({ 'href': info.imgUrl, 'download': info.av });
                $("#other").attr({ 'href': 'http://www.jijidown.com/API/AvToCid/' + info.avhm + '/' });
                $("#foot_Look").attr('href', 'https://www.bilibili.com/video/' + info.av + '/');
                $("#foot_baiduImg").attr('href', 'http://image.baidu.com/n/pc_search?queryImageUrl=http:' + info.imgUrl + '&querySign=&simid=&fm=index&pos=&uptype=paste');
                $("#foot_sauImg").attr('href', 'http://saucenao.com/search.php?database=999&file&frame=1&hide=0&url=' + info.imgUrl + '&urlify=on');
                $("#foot_tinImg").attr('href', 'https://www.tineye.com/search?url=http:' + info.imgUrl);
                $("#foot_whatImg").attr('href', 'https://whatanime.ga/?url=https:' + info.imgUrl);
                $("#foot_multiImg").attr('href', 'http://www.iqdb.org/?url=' + info.imgUrl);
            } else {
                $("#error").slideDown(500);
                setTimeout(function () { $("#main").removeClass().addClass('animated zoomOutRight').slideUp(); }, 50);
                $("#error").removeClass().addClass('animated rotateIn').attr('style', 'display:block');
            }
        });
}

$(function () {
    $("#hj").click(function () {
        if ($('img[src="/Content/ewm.png"]').length == 0) {
            var $toastContent = $('<img src="/Content/ewm.png" />');
            Materialize.toast($toastContent, 20000);
        }
    });
    $('#foot_Copy').click(function () {
        var clipboard = new Clipboard('#foot_Copy');
        clipboard.on('success',
            function (e) {
                Materialize.toast('复制成功', 3000, 'rounded');
                e.clearSelection();
            });
        clipboard.on('error', function (e) {
            Materialize.toast('复制失败，右键图片查看网址', 3000, 'rounded');
        });
    });
    $("#foot_DownImg").click(function () {
        Materialize.toast('如果这句话消失后还没有启动下载，那请手动另存为', 5000, 'rounded');
    });

});